package com.hugong.yxx.wksp.common.utils;

import cn.hutool.core.util.StrUtil;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.Trigger;
import org.springframework.scheduling.TriggerContext;
import org.springframework.scheduling.annotation.SchedulingConfigurer;
import org.springframework.scheduling.config.ScheduledTaskRegistrar;
import org.springframework.scheduling.support.CronTrigger;
import org.springframework.stereotype.Component;

import java.util.Date;

/**
 * @Author：
 * @Description：定时时间可配置化的定时器
 * @Date:
 * @Modified By:
 */
@Component
public class DynamicTask implements SchedulingConfigurer {
    /**
     * 默认定义一个CRON模板,结合
     * {@code period}属性组合
     */
    final static String CRON_TEMPLATE_DEFAULT="0 0/{} * * * ?";
    @Value("${period}")
    String period;

    /*@Autowired
    private SyncComponent loginController;*/

    @Override
    public void configureTasks(ScheduledTaskRegistrar taskRegistrar) {
        taskRegistrar.addTriggerTask(new Runnable() {
            //执行业务逻辑
            @Override
            public void run() {
                /*loginController.entrance();*/
            }
        }, new Trigger() {
            // 触发器
            @Override
            public Date nextExecutionTime(TriggerContext triggerContext) {
                CronTrigger trigger = new CronTrigger(StrUtil.format(CRON_TEMPLATE_DEFAULT,period));
                return trigger.nextExecutionTime(triggerContext);
            }
        });
    }
}
